const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res.data = await getData(data);
				break;
			case 'queryOne':
				res.data = await getDataOne(data);
				break;
			case 'queryTwo':
				res.data = await getDataTwo(data);
				break;
			case 'queryThree':
				res.data = await getDataThree(data);
				break;
			case 'queryById':
				res.data = await getDataById(data);
				break;
			case 'add':
				res = await addData(data);
				break;
			case 'edit':
				res = await editData(data);
				break;
			case 'editByName':
				res = await editDataByName(data);
				break;
			case 'editByNum':
				res = await editDataByNum(data);
				break;
			case 'editByDetail':
				res = await editDataByDetail(data);
				break;
			case 'del':
				res = await delData(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}
/**
 * 获取列表数据
 */
async function getData(data) {
	var result = null;
	const dbCmd = db.command
	if (data.type == 1) { //type  1表示查询所有 2表示带有查询条件的  3表示按照id查询一条记录  4排序查询 
		result = await db.collection('goods')
			.where({
				proData: {
					showState: data.state == '全部' ? dbCmd.neq('null') : data.state
				}
			})
			.skip(data.offset)
			.limit(6)
			.orderBy('_id', 'desc')
			.get()
	} else if (data.type == 2) {
		result = await db.collection('goods')
			.where({
				proName: eval('/' + data.productName + '/'),
				proData: {
					showState: data.state == '全部' ? dbCmd.neq('null') : data.state
				}
			})
			.skip(data.offset)
			.limit(6)
			.orderBy('_id', 'desc')
			.get()
	} else if (data.type == 3) {
		result = await db.collection('goods')
			.doc(data.id)
			.get()
	} else if (data.type == 4) {
		if (data.productName == '') {
			result = await db.collection('goods')
				.where({
					proData: {
						showState: data.state == '全部' ? dbCmd.neq('null') : data.state
					}
				})
				.skip(data.offset)
				.limit(6)
				.orderBy(data.field, data.order)
				.get()
		} else {
			result = await db.collection('goods')
				.where({
					proName: eval('/' + data.productName + '/'),
					proData: {
						showState: data.state == '全部' ? dbCmd.neq('null') : data.state
					}
				})
				.skip(data.offset)
				.limit(6)
				.orderBy(data.field, data.order)
				.get()
		}

	} else if (data.type == 5) {
		result = await db.collection('goods')
			.where({
				proData: {
					showState: '上架',
					groupList: dbCmd.neq('[]')
				}
			})
			.get()
	}
	return result.data
}
/**
 * 获取列表数据
 */
async function getDataOne(data) {
	const result = await db.collection('goods')
		.doc(data.id)
		.get()
	return result.data
}
/**
 * 获取列表数据
 */
async function getDataTwo(data) {
	var result = await db.collection('goods')
		.where({
			proData: {
				showState: '上架',
				groupList: dbCmd.neq('[]')
			}
		})
		.get()
	return result.data
}
/**
 * 获取列表数据
 */
async function getDataThree(data) {
	const result = await db.collection('goods')
		.where({
			proName: eval('/' + data.name + '/'),
			proData: {
				showState: ((data.state == ''||data.state=='全部') ? dbCmd.neq('null') : data.state)
			}
		})
		.skip((data.page-1)*data.limit)
		.limit(data.limit)
		.orderBy('updatetime', 'desc')
		.get()
	const resultTotal = await db.collection('goods')
		.where({
			proName: eval('/' + data.name + '/'),
			proData: {
				showState: ((data.state == ''||data.state=='全部') ? dbCmd.neq('null') : data.state)
			}
		})
		.count()
	
	let res = {
		total:resultTotal.total,
		records:result.data
	}
	return res
}
/**
 * 获取数据通过ID
 */
async function getDataById(data) {
	var result = await db.collection('goods_group').where({
			'groName': data.groName
		})
		.orderBy('_id', 'desc')
		.get()
	return result.data
}
/**
 * 添加数据
 */
async function addData(data) {
	var res = {}
	const result = await db.collection('goods').add({
		proName: data.proData.productName,
		price: data.proData.skuRetailPrice,
		sales: data.proData.sales,
		proData: data.proData,
		saleNum: 0,
		viewNum: 0,
		updatetime: data.updatetime
	})
	res.data = result
	res.code = 200
	return res;
}
/**
 * 更新数据
 */
async function editData(data) {
	var res = {}
	const result = await db.collection('goods').doc(data.id).update({
		proData: {
			showState: data.state
		},
		updatetime: data.updatetime
	})
	if (result.updated === 1) {
		res.code = 200;
		res.data = '更新数据成功';
	} else {
		res.code = 402;
		res.msg = '保存失败，请重新操作'
	}
	return res;
}

/**
 * 更新数据
 */
async function editDataByName(data) {
	let res = {}
	const result = await db.collection('goods').doc(data.id).update({
		proName: data.proData.productName,
		price: data.proData.skuRetailPrice,
		sales: data.proData.sales,
		proData: data.proData,
		updatetime: data.updatetime
	})
	res.data = result
	res.code = 200
	await setDiyPageValue(data.id)
	return res
}

/**
 * 更新数据
 */
async function editDataByNum(data) {
	let res = {}
	const result = await db.collection('goods').doc(data.id).update({
		viewNum: data.viewNum,
		updatetime: data.updatetime
	})
	res.data = result
	res.code = 200
	return res
}

/**
 * 更新数据
 */
async function editDataByDetail(data) {
	let res = {}
	const result = await db.collection('goods').doc(data.id).update({
		proDetail: data.proDetail,
		updatetime: data.updatetime
	})
	res.data = result
	res.code = 200
	return res
}

/**
 * @param {Object} data
 * 删除数据
 */
async function delData(data) {
	var res = {}
	var tmp = await db.collection('goods').where({
		'_id': data.id
	}).remove()
	if (tmp.deleted === 1) {
		res.code = 200
		res.data = '删除成功'
	} else {
		res.code = 402
		res.data = '删除失败'
	}
	return res
}

async function setDiyPageValue(id) {
	const result2 = await db.collection('goods').doc(id).get()
	const result3 = await db.collection('page').get()
	for (var i = 0; i < result3.data.length; i++) {
		if (result3.data[i].diyData.length > 0) {
			let tmp = result3.data[i].diyData;
			for (var j = 0; j < tmp.length; j++) {
				if (tmp[j].proList != undefined && tmp[j].proList.length > 0) {
					for (var k = 0; k < tmp[j].proList.length; k++) {
						if (tmp[j].proList[k]._id == result2.data[0]._id) {
							tmp[j].proList[k] = result2.data[0]
						}
					}
				}
			}
			const result4 = await db.collection('page').doc(result3.data[i]._id).update({
				diyData: tmp
			})
		}
	}
}
